﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml;
using System.Xml.Serialization;


namespace SusyGame_Model
{
    [Serializable]
    public class Enemy : Sprite
    {
        //Atributos
        protected float startHealth;
        protected float currentHealth;
        protected bool alive = true;
        protected float speed = 0.5f;
        protected int bountyGiven;

        protected Pool pool;
        protected Ground ground;

        private Queue<Vector2> waypoints = new Queue<Vector2>();

        //Propiedades
        public float CurrentHealth
        {
            get { return currentHealth; }
            set { currentHealth = value; }
        }

        public int BountyGiven
        {
            get { return bountyGiven; }
            set { bountyGiven = value; }
        }

        public bool IsDead
        {
            get { return !alive; }
        }

        //Constructor
        public Enemy(Texture2D texture, Vector2 position, float health, int bountyGiven, float speed)
            : base(texture, position)
        {
            this.startHealth = health;
            this.currentHealth = startHealth;
            this.bountyGiven = bountyGiven;
            this.speed = speed;
        }

        public Enemy()
        {
        }

        //Métodos
        public void SetWaypoints(Queue<Vector2> waypoints)
        {
            foreach (Vector2 waypoint in waypoints)
                this.waypoints.Enqueue(waypoint);

            base.Position = this.waypoints.Dequeue();
        }

        public float DistanceToDestination
        {
            get { return Vector2.Distance(base.Position, waypoints.Peek()); }
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (waypoints.Count > 0)
            {
                if (DistanceToDestination < speed)
                {
                    base.Position = waypoints.Peek();
                    waypoints.Dequeue();
                }

                else
                {
                    Vector2 direction = waypoints.Peek() - base.Position;
                    direction.Normalize();

                    base.Velocity = Vector2.Multiply(direction, speed);

                    base.Position += base.Velocity;
                }
            }

            else
                alive = false;

            if (CurrentHealth <= 0)
                alive = false;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (alive)
            {
                float healthPercentage = (float)CurrentHealth / (float)startHealth;

                Color color = new Color(new Vector3(1 - healthPercentage, 1 - healthPercentage, 1 - healthPercentage));

                base.Draw(spriteBatch);
            }
        }
    }
}
